import { CACHE_CONFIG, MASTER_CONFIG } from "./framework/const/Const";
import { Tools } from "./framework/utils/Tools";
import { CacheServer } from "./servers/cache/CacheServer";
import { LogicServer } from "./servers/logic/LogicServer";
import { MasterService } from "./servers/master/MasterService";


const args: string[] = process.argv.slice(2);
import cluster from 'cluster';


let startServer = async () => {
    if(cluster.isMaster){
        const serviceType: string = args[0];
    
        switch (serviceType) {
            case "logic": {
                const server: LogicServer = new LogicServer();
                server.start(await Tools.getRPCPort(3000));
                break;
            }
            case "cache": {
                const server: CacheServer = new CacheServer();
                server.start(CACHE_CONFIG.PORT);
                break;
            }
            case "master": {
                MasterService.inst.start(MASTER_CONFIG.PORT);
                break;
            }
            case "all":{
                cluster.fork({ servicetype: "master" });
                cluster.fork({ servicetype: "cache" });
                cluster.fork({ servicetype: "logic" });

                break;
            }
        }
    }
    else{
        const serviceType:string = process.env.servicetype;


        switch (serviceType) {
            case "logic": {
                const server: LogicServer = new LogicServer();
                server.start(await Tools.getRPCPort(3000));
                break;
            }
            case "cache": {
                const server: CacheServer = new CacheServer();
                server.start(CACHE_CONFIG.PORT);
                break;
            }
            case "master": {
                MasterService.inst.start(MASTER_CONFIG.PORT);
                break;
            }
        }
    }


    


}
startServer();


